
package com.njrsrc.android.app;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.njrsrc.R;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.ptr.PullToRefreshListView;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.utils.Helpers;

/**
 * 档案列表页面
 * 
 * @author Administrator
 */
public class ArchivesListActivity extends BaseSampleActivity implements OnClickListener {
    private TitleBar mBar;
    private PullToRefreshListView archivesListView;
    private List<UserInfo> resultList;
    private ArchivesListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archives_list);
        resultList = ArchivesActivity.tempList;
        initView();
    }

    private void initView() {
        mBar = (TitleBar) findViewById(R.id.activity_title_bar);
        mBar.setTitleText("档案列表");
        mBar.back.setOnClickListener(this);
        archivesListView = (PullToRefreshListView) findViewById(R.id.archives_listView);
        adapter = new ArchivesListAdapter(ArchivesListActivity.this);
        archivesListView.setAdapter(adapter);
    }

    class ArchivesListAdapter extends BaseAdapter {
        private Context mContext;
        private LayoutInflater inflater;

        public ArchivesListAdapter(Context mContext) {
            this.mContext = mContext;
            inflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            return resultList.size();
        }

        @Override
        public Object getItem(int position) {
            return resultList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = new Holder();
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_list_archive, null);
                holder.id = (TextView) convertView.findViewById(R.id.arch_id);
                holder.name = (TextView) convertView.findViewById(R.id.arch_name);
                holder.birthday = (TextView) convertView.findViewById(R.id.arch_birthday);
                holder.date = (TextView) convertView.findViewById(R.id.arch_date);
                holder.source = (TextView) convertView.findViewById(R.id.arch_source);
                holder.location = (TextView) convertView.findViewById(R.id.arch_location);
                holder.status = (TextView) convertView.findViewById(R.id.arch_state);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            UserInfo u = resultList.get(position);
            if (u != null) {
                if (!TextUtils.isEmpty(u.getDang_an_id())) {
                    holder.id.setText(u.getDang_an_id());
                }
                if (!TextUtils.isEmpty(u.getName())) {
                    holder.name.setText(u.getName());
                }
                if (!TextUtils.isEmpty(u.getBirthday())) {
                    holder.birthday.setText(Helpers.getStandardTimeStr(mContext, u.getBirthday()));
                }
                if (!TextUtils.isEmpty(u.getArchived_date())) {
                    holder.date.setText(Helpers.getStandardTimeStr(mContext, u.getArchived_date()));
                }
                if (!TextUtils.isEmpty(u.getSource())) {
                    holder.source.setText(u.getSource());
                }
                if (!TextUtils.isEmpty(u.getDang_an_location())) {
                    holder.location.setText(u.getDang_an_location());
                }
                if (!TextUtils.isEmpty(u.getStatus())) {
                    holder.status.setText(u.getStatus());
                }
            }
            return convertView;
        }
    }

    static class Holder {
        TextView id, name, birthday, date, source, location, status;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tb_ibtn_back:
                finish();
                break;
        }
    }
}
